package com.hzncc.flowable_diboot.flowable.servicetask;

import com.diboot.iam.entity.IamUser;
import com.diboot.iam.service.IamUserService;
import com.hzncc.flowable_diboot.util.JsonUtils;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import jakarta.annotation.Resource;
import java.io.File;
import java.util.Map;

/**
 * @Description :
 * @Date : 2024/7/11
 * @Author : ChenJin
 * @Version : v1.0.0
 **/
@Service
public class ContractPushOAServiceTask implements JavaDelegate {


    @Value("${oa.url}")
    private String url;

    @Resource
    private RuntimeService runtimeService;
    
    @Resource
    private IamUserService iamUserService;

    @Override
    public void execute(DelegateExecution delegateExecution) {

        RestTemplate restTemplate = new RestTemplate();

        //获取当前流程实例的流程变量
        Map<String, Object> variables = runtimeService.getVariables(delegateExecution.getProcessInstanceId());
        //获取当前流程实例所绑定的合同审批信息
        //根据processInstanceId查询合同审批contract_audit表
        //获取合同基础信息
        String contractCode = "";
        String customerName = "";
        String contractCreateBy = "";
        IamUser createUser = iamUserService.getEntity(contractCreateBy);
        String userNum = createUser.getUserNum();

        String jsonString = JsonUtils.toJsonString(variables);


        //把SysNewsParamterForm转换成MultiValueMap
        MultiValueMap<String,Object> wholeForm = new LinkedMultiValueMap<>();
        wholeForm.add("docSubject", contractCode + customerName);
        wholeForm.add("docCreator", "{\"PersonNo\":\"00013\"}");
        wholeForm.add("docContent", "");
        wholeForm.add("docStatus", 20);
        wholeForm.add("fdTemplateId", "169b437e7e1cd1bd7b6367c4563b24ef");
        wholeForm.add("formValues", jsonString);


        LinkedMultiValueMap<String,Object> innerMap = new LinkedMultiValueMap<>();
        //注意附件列表的key是一样的
        wholeForm.add("attachmentForms[0].fdKey", "fd_3731bf4852e8fe");//第一个附件
//        wholeForm.add("attachmentForms[0].fdFileName", new String("测试文档.txt".getBytes("UTF-8"),"ISO-8859-1"));
        wholeForm.add("attachmentForms[0].fdAttachment", new FileSystemResource(new File("E:/测试文档.txt")));


        HttpHeaders headers = new HttpHeaders();
        //如果EKP对该接口启用了Basic认证，那么客户端需要加入
        //addAuth(headers,"yourAccount"+":"+"yourPassword");是VO，则使用APPLICATION_JSON
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        //必须设置上传类型，如果入参是字符串，使用MediaType.TEXT_PLAIN；如果
        HttpEntity<MultiValueMap<String,Object>> entity = new HttpEntity<MultiValueMap<String,Object>>(wholeForm,headers);


        //有返回值的情况 VO可以替换成具体的JavaBean
        ResponseEntity<String> obj = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
        String body = obj.getBody();

    }
}
